package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

//套接字：网络编程(服务端和客户端)
//将两个DML套接
//网络通信--试试做一个聊天软件
public class SocketServer {
		public void oneServer() throws IOException{
			//1- 创建一个socket服务器，指定端口号
			ServerSocket server = new ServerSocket(5209);	//服务端对应的socket(端口号随意,但不能冲突)
			System.out.println("服务端启动");
			//2- 等待客户端进行访问,开启监听
			//接收socket
			Socket socket = server.accept();	//等待客户端发来一个连接
			System.out.println(socket);			//没有输出，因为cpu阻塞server.accept()
			//3- 把从客户端接收到的数据打印出来
			socket.getInputStream();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			System.out.println(in.readLine());
//			PrintWriter write = new PrintWriter(socket.getOutputStream());
			//4- 把服务器的内容传回客户端
			PrintWriter write = new PrintWriter(socket.getOutputStream());
			write.println("def");
			write.flush();
			write.close();
			
		}
		public static void main(String[] args) throws IOException {
			SocketServer ss = new SocketServer();
			ss.oneServer();
		}
}
